dojo.provide('app.search');
dojo.require('app.i18n');
dojo.requireLocalization('app.i18n');
dojo.require('app.utils.utils');
dojo.require('app.model');
dojo.require('dijit.layout.TabContainer');
dojo.require('dojox.layout.ContentPane');
dojo.require('dijit.form.Form');
dojo.require('dijit.form.TextBox');
dojo.require('dijit.form.Textarea');
dojo.require('dijit.form.Button');
dojo.require('dijit.form.CheckBox');
dojo.require('app.form.SubmitTextBox');
dojo.require('app.form.RegisterItem');
dojo.require('app.form.AddressGeoItem');
dojo.require('dojo.store.JsonRest');
dojo.require('dijit.form.ComboBox');
dojo.require('dojox.data.JsonRestStore');
dojo.require('dijit.TitlePane');
dojo.require('app.form.ResultsPane');
dojo.require('app.form.FilteringSelect');

dojo.declare('app.search', [], {
	_wdgIds : [ 'nameInput', 'addressInput', 'countryInput', 'confirmed' ],
	updateList : function() {
		var params = {
			name : dijit.byId('nameInput').get('value'),
			address : dijit.byId('addressInput').get('value'),
			country : dijit.byId('countryInput').get('value')
		}
		// var _f = dojo;
		// app.utils
		// var query = _f.objectToQuery(params);
		// var url = Services.search.url + "?" + query;
		// dijit.byId('searchPane').set('href', url);
		dijit.byId('searchResidentsPane').fetch({
			query : params
		});
		dijit.byId('searchIndicationsPane').fetch({
			query : params
		});
	},
	_load : function(data, ioargs) {
		console.log('loaded');
		console.log(data);
		this.updateList();
	},
	_error : function(err) {
		console.log('error');
		console.log(err);
	},
	_quickAdd : function() {
		dojo.publish('view/add', [ app.mvc.View.get('search').data(), true ]);
	},
	startup : function() {
		var bundle = app.i18n.getLocalization('app.i18n');
		var _self = this;

		var onValidInput = function(e) {
			_self.updateList();
		};

		var name = new app.form.SubmitTextBox({
			name : 'contact.name',
			placeHolder : bundle.get('search_placeholder',
					'register_label_name'),
			onValidInput : onValidInput
		}, 'nameInput');

		console.log(this.declaredClass + '.name initiated');

		var addr = new app.form.SubmitTextBox({
			name : 'search.address',
			placeHolder : bundle.get('search_placeholder',
					'register_label_address'),
			onValidInput : onValidInput
		}, 'addressInput');

		console.log(this.declaredClass + '.address initiated');

		var country = new app.form.FilteringSelect({
			name : 'contact.country.id',
			placeHolder : bundle.get('search_placeholder',
					'register_label_country'),
			store : Services.country.store(),
			labelAttr : 'description',
			searchAttr : 'description',
			onChange : onValidInput
		}, 'countryInput');
		console.log(this.declaredClass + '.country initiated');

		var conf = new dijit.form.CheckBox({
			name : 'type',
			value : 'true'		
		}, 'confirmed');
		console.log(this.declaredClass + '.confirmed initiated');
		dojo.connect(conf, 'onChange', onValidInput)

		var bt = new dijit.form.Button({
			label : bundle.get('quick_add_label'),
			onClick : function(e) {
				_self._quickAdd();
			}
		}, 'quickAdd');
		console.log(this.declaredClass + '.quickAdd initiated');
		
		var view = app.mvc.View.get('search');
		for(id in this._wdgIds){
			var wdg = dijit.byId(this._wdgIds[id]);
			view.bind({widget:wdg});
		}
		var searchResidents = new app.form.ResultsPane({
			store : Services.resident.store(),
			createItem : function(item, node) {
				var _item = item.i;
				new app.form.RegisterItem({
					item : _item,
					onDeleteClick : function() {
						this.service.Delete(this.item);
					},
					onViewClick : function() {
						var pruned = app.model.prune(this.item);
						dojo.publish('view/add', [ pruned,true ]);
					}
				}, dojo.create('div', {}, node));
			}
		}, 'searchResidentsPane');
		searchResidents.startup();

		var searchIndications = new app.form.ResultsPane({
			store : Services.indication.store(),
			createItem : function(item, node) {
				new app.form.RegisterItem({
					item : _item,
					onDeleteClick : function() {
						this.service.Delete(this.item);
					},
					onViewClick : function() {
						var pruned = app.model.prune(this.item);
						dojo.publish('view/add', [ pruned ]);
					}
				}, dojo.create('div', {}, node));
			}
		}, 'searchIndicationsPane');
		searchIndications.startup();

		console.log(this.declaredClass + '.searchPane initiated');

		this.updateList();
	}
});
app.search.page = new app.search();
